/*
 caculate the fft frequency spectrum of a given file, 
 and sort out the 8 maximum tuples of power and location
 written by He Dongjing, Shanghai Jiao Tong University
*/
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "fft.h"

using namespace std;

int main(int argc, char *argv[]) {

	int err = 0;
	// +1 to append a null char
	unsigned char* source = (unsigned char*)calloc(sizeof(unsigned char), MAXLEN+1);
	unsigned int length = 0;
	ifstream ifs;

	// open file
	if(argc<=1) printf("Usage: ./test [filename]\n");
	if(argc>1) {
		printf("open file: %s\n", argv[1]);
		ifs.open(argv[1]);
	}
	if(ifs.is_open()){
		ifs.read((char*)source,MAXLEN); 
		length = ifs.gcount();
		ifs.close();
	} 

	// print the len of string, and the file size
	cout << "len is " << strlen((const char*)source) << "& length is " << length << endl;

	//...Inside your signal processing function
	//   Allocate memory for signal data
	complex *pSignal = new complex[MAXLEN];

	//...Fill signal array with data
	for(unsigned int i=0; i<length; i++) {
		pSignal[i] = (double)source[i];
	}

	//   Apply FFT
	CFFT::Forward(pSignal, length);

	// sort and get 8 maximum two-tuples
	feature_lib *lib;
	ShowFeature(pSignal, length, lib);

	//   Free memory
	delete[] pSignal;
	return err;
}
